/*
 * sysinit.c
 *
 *  Created on: 13.05.2011
 *      Author: Eugen Schneider
 */

#include <stdint.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>

#include "sysinit.h"

bool bInitIO(void) {

	DDRA = 0xFF; /*PORTA as output*/
	PORTA = 0x00; /*PORTA low*/

	DDRB = 0xFF; /*PORTB as output*/
	PORTB = 0x00; /*PORTB low*/

	return true;
}

bool bInitODOsensor(void) {

	DDRA &= ~((1 << PA7) | (1 << PA6)); /*Pins PA7 and PA6 as input*/
	PORTA |= ((1 << PA7) | (1 << PA6)); /*Pins PA7 and PA6 pulled up*/

	return true;
}

bool bInitGsensor(void) {

	DDRA &= ~((1 << PA1) | (1 << PA0)); /*Pins PA1 and PA0 as input*/
	PORTA |= ((1 << PA1) | (1 << PA0)); /*Pins PA1 and PA0 pulled up*/

	return true;
}

bool bInitTimer(void) {


	TCCR0 = ((1 << CS01)|(1 << CS00));/*timer0 with 64 prescaler*/
	TCNT0 = 0x06;
	TIMSK |= (1 << TOIE0); /*timer0 overflow interrupt enabled*/



	return true;
}

bool bInitADC(void) {

	uint16_t wResult;

	ADMUX = (uint8_t) ~((1 << REFS1) | (1 << REFS0) | (1 << ADLAR)); /*AVCC as reference-voltage, result is
	 right adjustive in ADCL and ADCH*/

	ADCSR = ((1 << ADPS2) | (1 << ADPS0) | (1 << ADPS1)); /*ADC with 125kHz*/
	ADCSR |= ((1 << ADEN) | (1 << ADSC)); /*enable ADC and start a dummy conversion*/
	do {

	} while ((ADCSR & (1 << ADIF))); /*wait until end of conversion*/

	wResult = ADCW; /*read and discard result*/

	return true;
}
